<template>
  <div>

    <div style="margin-top: 15px;">
      <!--    添加按钮-->
      <el-button type="primary" icon="el-icon-plus" style="margin: 10px 0px" @click="dialogFormVisibleAdd = true">添加
      </el-button>
      <el-input
        style="margin-left: 200px;width: 1000px"
        placeholder="请输入要查询的商品可以根据名称查询"
        v-model="input3.name"
        class="input-with-select"
      >
        <el-button slot="append" icon="el-icon-search" @click="souSelect(input3.name)"></el-button>
      </el-input>
    </div>

    <!--
      表格组件
      data:表格组件将来需要展示的数据-----数组类型
      border:给表格添加边框
      column属性
      label:展示标题
      prop：对应列内容的字段名
      width:对应列的宽度
      align:标题的对齐方式
    -->
    <el-table
      border
      :data="list"
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="商品id"
        width="120"
        align="center"
      >
      </el-table-column>
      <!--      prop="tmName"-->
      <el-table-column
        width="140"
        prop="name"
        label="名称"
        align="center"
      >
        <!--        prop="logoUrl"-->
      </el-table-column>
      <el-table-column
        width="180"
        prop="description"
        label="描述"
        align="center"
      >
      </el-table-column>
      <el-table-column
        width="140"
        prop="price"
        label="价格"
        align="center"
      >
      </el-table-column>
      <el-table-column
        width="140"
        prop="stock"
        label="库存"
        align="center"
      >
      </el-table-column>
      <el-table-column
        width="140"
        prop="categoryleveloneId"
        label="分类1"
        align="center"
      >
      </el-table-column>
      <el-table-column
        width="140"
        prop="categorylevelthreeId"
        label="分类2"
        align="center"
      >
      </el-table-column>
      <el-table-column
        width="140"
        prop="categoryleveltwoId"
        label="分类3"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="fileName"
        label="文件名称"
        align="center"
      >
        <!--插入图片链接的代码-->
        <template slot-scope="{row}">
          <el-image :src="require('@/static/img/'+row.fileName)"/>
        </template>
      </el-table-column>
      <el-table-column
        prop="province"
        label="操作"
        align="center"
      >
        <template slot-scope="scope">
          <el-button type="warning" icon="el-icon-edit" @click="showUpdate(scope.row)">修改</el-button>
          <el-button type="danger" @click="deleteClick(scope.row)" icon="el-icon-close">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--
        分页器
        当前第几页,数据总页数，每一页展示条数，连续页码数
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"

        :current-page代表当前第几页
        :total代表分页器需要展示数据的条数
        :page-size代表分页器每页需要展示的条数
        :page-sizes代表可以设置每页需要展示的条数
        layout:可以实现分页器布局
        pager-count按钮的数量 如果是6连续页码是4
    -->
    <el-pagination
      style="margin-top: 20px;textAlign:center"
      :current-page="1"
      :total="total"
      :page-size="5"
      :page-sizes="[3,5,10]"
      layout="prev, pager, next,jumper,->,sizes,total"
      @current-change="page"
    >
    </el-pagination>

    <!--    添加对话框-->
    <el-dialog title="添加商品" :visible.sync="dialogFormVisibleAdd">
      <el-form ref="form" :rules="rules" :model="form" label-width="80px" style="width: 80%">
        <el-form-item label="名称:" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="描述:" prop="description">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="价格:" prop="price">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="库存:" prop="stock">
          <el-input v-model="form.stock"></el-input>
        </el-form-item>
        <el-form-item label="分类1:" prop="categoryleveloneId">
          <el-input v-model="form.categoryleveloneId"></el-input>
        </el-form-item>
        <el-form-item label="分类2:" prop="categoryleveltwoId">
          <el-input v-model="form.categoryleveltwoId"></el-input>
        </el-form-item>
        <el-form-item label="分类3:" prop="categoryleveltwoId">
          <el-input v-model="form.categorylevelthreeId"></el-input>
        </el-form-item>
        <el-form-item label="文件名称:" prop="fileName">
          <el-input v-model="form.fileName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisibleAdd = false;addData()">添加</el-button>
      </div>
    </el-dialog>

    <!--    修改对话框-->
    <el-dialog title="修改商品" :visible.sync="dialogFormVisible">
      <el-form ref="form" :rules="rules" :model="form" label-width="80px" style="width: 80%">
        <el-form-item label="商品id:" prop="id">
          <el-input v-model="form.id"></el-input>
        </el-form-item>
        <el-form-item label="名称:" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="描述:" prop="description">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="价格:" prop="price">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="库存:" prop="stock">
          <el-input v-model="form.stock"></el-input>
        </el-form-item>
        <el-form-item label="分类1:" prop="categoryleveloneId">
          <el-input v-model="form.categoryleveloneId"></el-input>
        </el-form-item>
        <el-form-item label="分类2:" prop="categorylevetwoId">
          <el-input v-model="form.categoryleveltwoId"></el-input>
        </el-form-item>
        <el-form-item label="分类3:" prop="categorylevelthreeId">
          <el-input v-model="form.categorylevelthreeId"></el-input>
        </el-form-item>
        <el-form-item label="文件名称:" prop="fileName">
          <el-input v-model="form.fileName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false;shuaxin()">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false;updateData()">修改</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'TradeMark',
  data() {
    return {
      //  列表展示的数据
      list: [],
      total: null,
      form: {
        id: '',
        name: '',
        description: '',
        price: '',
        stock: '',
        categoryleveloneId: '',
        categoryleveltwoId: '',
        categorylevelthreeId: '',
        fileName: ''
      },
      // 修改对话框显示与隐藏的属性
      dialogFormVisible: false,

      // 添加对话框显示与隐藏的属性
      dialogFormVisibleAdd: false,

      // 校验规则
      rules: {
        // required:必须要验证字段(前面五角星有关系) message:提示信息 trigger:用户设置行为(事件的设置:blur,change)
        id: [
          { required: true, message: '请输入商品Id必填项', trigger: 'blur' },
          // 商品id长度1-6
          { min: 1, max: 6, message: '长度在1-6必填项商品Id', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入商品名称必填项', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在2到6必填项商品名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入商品描述', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入商品价格可以是小数必填项', trigger: 'blur' }
        ],
        stock: [
          { required: true, message: '请输入商品库存必填项', trigger: 'blur' }
        ],
        categoryleveloneId: [
          { required: true, message: '请输入商品分类1', trigger: 'blur' }
        ],
        categoryleveltwoId: [
          { required: true, message: '请输入商品分类2', trigger: 'blur' }
        ],
        categorylevelthreeId: [
          { required: true, message: '请输入商品分类3', trigger: 'blur' }
        ],
        fileName: [
          { message: '请输入图片名称.jpg', trigger: 'blur' }
        ]

      },

      // 搜索
      input3: {
        // id: '',
        name: ''
      },
      select: ''

    }
  },

  methods: {
    // 分页
    page(currentPage) {
      axios.get('http://localhost:8081/product/page/' + currentPage + '/5').then((req) => {
        console.log(req)
        //  将查询到的数据放到lsit数组中
        this.list = req.data.records
        this.total = req.data.total
      })
    },

    //  删除方法
    deleteClick(row) {
      console.log(row)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.get('http://localhost:8081/product/delete/' + row.id).then((req) => {
          console.log(req)
          // 刷新
          window.location.reload()
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 修改  弹框和回显数据
    showUpdate(row) {
      // 显示对话框
      this.dialogFormVisible = true
      this.form = row
      // console.log(row)
    },
    // 点击取消刷新页面
    shuaxin() {
      window.location.reload()
    },
    // 修改数据
    updateData() {
      axios.put('http://localhost:8081/product/update/' + this.form.id, this.form).then((req) => {
        console.log(req)
        console.log(this.form.id)
        // 刷新
        window.location.reload()
      })
    },

    // 添加数据
    addData() {
      axios.post('http://localhost:8081/product/insert', this.form).then((req) => {
        console.log(req)
        if (req.data === 1) {
          window.location.reload()
        }
      })
    },

    //  搜索功能
    souSelect(name) {
      console.log(name)
      axios.get('http://localhost:8081/product/getSelect', {
        params: {
          name: name
        }
      }).then((req) => {
        console.log(req)
        //  将查询到的数据放到lsit数组中
        this.list = req.data
        this.total = req.data.length
      })
    }

  },

  // 开始首页展示数据
  created() {
    axios.get('http://localhost:8081/product/page/1/5').then((req) => {
      console.log(req)
      //  将查询到的数据放到lsit数组中
      this.list = req.data.records
      this.total = req.data.total
    })
  }

}
</script>

<style scoped>

.el-select .el-input {
  width: 130px;
}

.input-with-select .el-input-group__prepend {
  background-color: #fff;
}

</style>
